﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using RegulatoryPlatform.DB;
using RegulatoryPlatform.Models;

namespace RegulatoryPlatform.ScheduleTask
{
    /// <summary>
    /// 互联网医院业务收入统计
    /// </summary>
    public class TJ_HLWYY_YWSR : Base
    {
        public override async Task Execute(IJobExecutionContext context)
        {
            string param = DateTime.Now.ToString("yyyyMMdd");
            var json = rDData.GetDataByMethod("Address_Get_YWSL", new YWSRRequest
            {
                YWSJ = param
            });

            var response = Serialize<YWSL>(json);
            if (response.status == 10000 && response.msg == "success")
            {
                try
                {
                    foreach (var item in response.content)
                    {
                        TB_TJ_HLWYY_YWSR entity = new TB_TJ_HLWYY_YWSR();
                        entity.YLJGDM = YLJGDM;
                        entity.KSBM = item.doctorOutId.Split('_')[0];
                        entity.WSJGDM = WSJGDM;

                        entity.HLWZLYPSR = item.fzypsr;
                        //下面是需要确认的数据
                        entity.HLWZLYBSR = 0;
                        entity.HLWZLZFSR = 0;
                        entity.HLWZLZSR = entity.HLWZLYPSR + entity.HLWZLYBSR + entity.HLWZLZFSR;
                        entity.XGBZ = "1";

                        db.Set<TB_TJ_HLWYY_YWSR>().Add(entity);
                    }
                    await db.BulkSaveChangesAsync();
                    await Console.Out.WriteLineAsync($"互联网医院业务收入 数据同步完成 在{DateTime.Now}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"互联网医院业务收入统计 出错了:{ex.Message}");
                    JobExecutionException e2 = new JobExecutionException(ex);
                    // 这个工作将立即重新开始  
                    e2.RefireImmediately = true;
                    throw e2;
                }
            }
            else
            {
                await Console.Out.WriteLineAsync($"未能找到 {param} 对应的 互联网医院业务收入");
            }
        }
    }

    /// <summary>
    /// 互联网医院 业务收入 入参
    /// </summary>
    public class YWSRRequest
    {
        /// <summary>
        /// 业务时间 YYYYMMDD
        /// </summary>
        public string YWSJ { get; set; }
    }

}
